code refactoring, tried to speed up