tested and writes to file