Added more documentation and cleaned up the code.