rewrite check into separate function