added comments, cleaned up code