Modified so it will not indefinitely loop upon failing to complete the puzzle