Make upgrade routine more robust