complete rewrite of ntiles