Made the code more rubust