refactor compile function