Updated error handling

write-error was used incorrectly for error handling.