update mask algorithm