Slightly optimized Problem 2.