Move utility functions to separate file. Minor refactoring