Added some documentation and cleaned up code