Code refactoring and review