Fixed logic bug in contract_end ranges.

Main code rev. 1849