refactored Problem 30