cleaned up code, added some comments