Improve code comments and style in lib code

* Fix indentation code style in places.
* Improve comment language and clarity.
* Use structured comment conventions to provide better descriptions of
  functions and their API.