Problem 4 - optimized