Fixed memory bugs