Add globals and separate out functions