Minor refactoring of dispose code