Cleaned up the code. Added some comments.