Refactored, optimized