Implemented some write out of bounds protection... which hopefully works as intended and doesn't break too much