code cleanup and refactoring