Test fix and refactoring