this feels a little spaghetti but I don't have a better idea in my mind