Improved documentation

Added documentation in areas where it was not found.
Refactored some code.