Refactor injected code into modules

Addresses feedback from #2