Rework alloc and destroy functions

Targets (and solves?) #142 .