Cleanup code and add documentation