Added comments, cleaned up the code