data munging refactoring (finally!)