optimization and refactoring