Renamed and made more robust