ugly as sin solution to problem 4.