Refactored data prep function