Move functions to class and refactor code