some refactoring to reduce duplication before starting #14