Improve documentation

Improved documentation in the code. Also fixed a few minor problems/bugs.