Fixed memory overwriting bug