Refactored, optimized, commented