updated matching engine to cover all cases (see design doc for more)