Memory corruption fix