Add documentation and some bug fixes