Made the code more robust