Issue #998: Fixed code formatting and comments

- Fixed the code so that it (hopefully) complies to existing coding
standards.
- Reordered some of the include and define statements.
- Added comments to the code explaining what is done.
Issue #998: Fixed typo in comment