cleaned up code a bit, added some comments