refactored but still slow