Refactor code to figure prime better