Refactored, improved performance