Fixed prime factorisation function and tidied up code.