Code-generation fatal error prevention

Supersedes #193 and #190