Code does not work for multiple prime factors