refactor chunks into functions