Algorithm is completely broken. Need to figure out a better way to solve it.