Simplified code using newly added err_exit function.