Defined error check loops in replace.py

Cleanup in others.