inner algorithm