Fix GetNextWorkRequired (part 1)

Better calculates median.
Also avoids an unlikely case that could have hypothetically stalled the chain in extreme circumstances.