Refactoring, moved helper functions into a separate mode