Pull out functions from where to top level