Refactor test code