fixed two potential infinite loops when recursion was used