Fixed variable scope, correct process exit, additional debug output, closes #3