Fix memory corruption errors