Added some more comments, cleaned up the code