Should be private, i moved the code due the main function is too large and complex and hard to read, but yes will be hard to merge.