move functions up to before where they are used