simple perfomance optimization and refactoring