moved code to advance to next block to a separate function. flattened if/else chain.