added some documentation and cleaned up code a bit