Cat vs. Dog puzzle solved and refactoring