Only write valid lines to file
Fixed merge conflicts