better error checking
fixed undefined behavior when reading from and writing to same file in one pipeline
merge