Added lots of robustness checks.