Refractored allocation code