Cleaned up the code. This is the version I have submitted.