refactoring ai code