Changed algorithm to use sleep where relevant.