Fixed some problems and did some cleanup
changed some comments to make it clearer