Added documentation and cleaned up codew