Refactored using the LeastDivisor function