Move error catching to global functions