implemented __into_modes slightly differently in order to speed up the algorithm