nicer solution to problem 5 with passing tests