refactor scheduler code