Refactoring and speed improvement for tests