initial refactoring for #14 and #15