Refactoring & performance improvements for test