Made code more robust for certain input parameters