Re-checking and cleaning up the code: most likely still have a bug in re-ordering...