Horribly incorrect algorithm