added a bit more failure checking