improvments to algorithm