Fixed some bugs due to recursion with global variables (bash scripting - aaagh!)