added a few comments and cleaned up code