Fix invalid memory access