Code refactoring and UI improvements