Started working on issue #85 (type guards on generated code)

This is not completely working, but I've no time today to finish this.