refactored finding matching parenthesis