Fix the segfault issue for when the EOS is reached. Also removed some unused code.