Added comments, cleaned up code a little