Can you refactor this so that it is not a global function? Maybe clojures could be used?