Refactoring and rewrite