Refactor rewrite