General refactoring and debugging