improve the code style, fix incorrect usage

This PR just improves the code style a bit, and fixes incorrect usage
of for-in loops, an extraneous set of braces, and some other things.

The behavior of the module should not change. Tests pass.