optimized solution to problem