Added some more failsafe code