clearer and faster solutions for popping the stack
fix file output

*use a tempfile to prevent clobbering the output file
 unless the fail happens when copying the result file
fix error handling and propagation in the generated source
fix error reporting in the generator
improve tracing and debugging capabilities of the generated source