optimizations to problem 14