Fixed memory overwrite bug