Completed problem 22, cleaned up some code