Reorganization to make the code easier to read and extend