changed algorithm to allow parenthesis