Refactors sieve function