Reads and writes to file properly