Made code more robust