Some debugging and refactoring