More Pythonic way for error checking