Refactorized and cleaned up the tests code.