Working on finding error in algorithm