added exit function and error check