rewrite of the (horrible) get_id function