some utilities and code refactoring