fix invalid memory writes