More test improvments and refactoring