Adding some documentation and slightly re-organizing the code.