Refactor and rewriting