Modified exception handling in refinement.py