algorithm sketch in comments