fix GetNextInMainChain (thanks Pieter Wuille for suggesting this)