Code cleanup

added comments and straightened up code