Code refactoring and reorganizing