Refactored/Optimized for the intended purpose.