Cleaning up the code, adding comments.