Refactor a few modules for better style

In this file you had everything happening within the scope of the exported function, which wasn't necessary. It's easier to read when you break it apart more.