Made routines more robust