Fix segfault on deserialization.
Add main-function generation.

This is extremely messy.
Fix incorrect trigger->function transformation in imperative.