Algorithm almost done--everything working as far as I can tell