Debugging hanging issues and refactoring code