Refactoring for speed improvement