Refactored labeling and weight computing code to be type-safe in the type and dimensions of the labels.