Improve testability and fix test failures