Made somewhat more robust