Some refactoring for better testability