Cleaning up the code, adding more documentation.