Added check for processing the end of a file
Added forcing processing the end of a file when replacing