Some (not enough) refactoring/cleanup