Reorganize code for maintainability