Fix mixed-up tests.
Refactor and add some new test cases