fixes and refactoring of fileRead and fileWrite