Refactored part of distance function that constructs feature vectors into its own function