Some attempts at fixing memory allocation