Fix ERANGE error and clean up exceptions