More refactoring and code beautification