Improved code robustness

Ditto