working but slow version of the cat vs dog problem