This patch includes a lot of work to refactor and reorganize the