Updated interleaving code to be more robust