Code reorganization
Some code reorganization and simplification