More refactoring and function inlining