Massively reorganize and cleanup the code

Should be no functional difference, except the explicit array improvements to --merge mode