removing debugging statements and refactoring duplicate code