added comments to the code and cleaned up