added a recursion checker (pure functional style)