Added more comments to code and cleaned up formatting