purge before loading module for running test cases + some code refactoring

This fixes the post-module generation failure when building GHC (which has GHC as build dependency, so conflicts occur).