Contract code + race conditions
This PR fixes additional race conditions, and reorganizes some of the contract code.
The short tests pass, the long tests hang indefinitely. didn't investigate.
There were a few subtle errors in the contract code, including <
instead of <=
, and true
instead of false