refactored for an external declaration, problems with StackOverflowException still remain