Fixed a write file check and added exit codes