Minor refactoring

These are not functional changes, but I they do reduce code duplication (the diff stats prove it!) and eliminate dependencies on deprecated Python modules like new.