changed parenthesis algorithm to make it easier to understand