Improve prime factor algorithm