rewrote error handling