Need to fix problems with algorithm