How do you stay on topic while coming up with good/clever bars and rhymes?

I usually come up with the end-rhymes (which a lot of people, myself included, do when freestyling) and might try and fill in some inner-rhymes, but then I'm generally just all over the place, which is cool, but I'd like to do something more topically focused. But I also feel a lot of people who do more topically focused stuff and like serious stuff can sometimes have some pretty trashy rhymes and flows (unless they're good, of course) cause they focus more on saying the right things and getting the story or point across rather than having it musically sound good. I wanna do both. Does anyone have any tips on how I can do both? Any techniques? How do you do it? Any kind of advice like "just practice" is welcome if it's true and it works for you and other people like the pros. I hope my question makes sense. Advice would be dope. Thank you