Improved documentation and some code cleanup