Misc changes: refactoring and code simplication