cleaned up code, added doc comments