Fix some typos
Fix handling of whitespace inside substitution, etc.
Fix unit test

Note that the only user-visible change with this branch is that "$(foo",
ie. a missing closing parentheses, will throw an exception.