Moved the recursive helper functions out of the class.