Further modularization and cleanup