Refined logic. Need to verify with Sandie, but I think close.
initial version
WIP