cleaned up, added some comments and fixed a few glitches