(Lakka) Code refactoring and performance improvements