Fix bad memory accesses