Add documentation and clean up code