Refactor tests into separate module

A number of cleanups and refactors to conform to best practices.