Factoring out allocation, fixes #24