algorithm works (sort of)