Refactoring, mostly to avoid duplicate code
Should have been added with previous refactoring commit