Fix unbound variables issue.

        And add tests for it.