better slice generation algorithm