helper function here? there's a lot going on here