add parenthesis handling and fix some memory leaks