fixed variable scope
Free things responsibly.
Handles spaces inside quotes properly.