Fixed memory management issues