trying to clean this up a little more
should be robust enough for gemini use case