Moved some functions around to remove errors