Cleanup modules and move `exports` to the top of files

This applies a pattern of moving `require()s` and `exports` to the top
of the module files.

More code comments have also been added.