Made the code safer in presence of failures