Fixed using mean on empty vectors
Re-organised to have same format as calculate_segment_rank