Refactoring, fixing side-effects