small changes to algorithm