fix bugs about EOF and memory references / thanks to potetisensei