Slightly optimized Problem 6.