code optimization and refactoring