added comments and fixed a few bugs