algorithm instead of loop