refactor rewrite_repos function