algorithm improvements and unit tests

Code looks more reliable now, but still has some known issues, like it does not see difference between brackets inside strings and operator brackets.