removing debuggins statements and refactoring duplicate code