Various code refactoring