Tidying up code, mallocs now safe