I can't remember where I first heard the teaching from the Baal Shem Tov about lifting up the sparks of strange thoughts. Here's how that teaching goes:
It's human nature for one's mind to wander, even at times of contemplation or prayer. This is just what it's like to have a human brain. And then it's easy to imagine that one should castigate oneself for those thoughts which are getting in the way of connecting with God.
Not so, said the Baal Shem Tov. When those thoughts arise, our task isn't to banish them or to kick ourselves for having them, but to lift them up. Even an "unholy" thing has a pure spark of divinity within; find that spark and elevate it. (The binarism of holy / unholy may feel foreign or old-fashioned; just roll with it, because I think the teaching transcends the binarism.)
For instance, if one is distracted during prayer by the thought of a beautiful woman (of course he was speaking to men in a heteronormative context -- translate as needed into your own appropriate milieu), one shouldn't knock oneself for being distracted by that beauty. The thing to do instead is to lift up those sparks to God by thanking God for the God-given beauty which distracted you -- to find a way to make the distraction itself a path toward the One.
It's human nature to be constantly getting caught up in remembering the past (whether bitter or sweet), in anticipating the future (whether bitter or sweet), in telling oneself stories, in daydreaming about one's fears and one's hopes and one's desires. My meditation practice has revealed to me the constant chatter my monkey mind provides on all of those fronts. All of those can arise while I'm falling asleep, or driving the car, or washing dishes... and all of those can happen while I'm attempting to sit in meditation or to daven, too.
It's easy to kick myself for that; to think that if I really had good focus I would be able to keep that stuff out of my mind while in meditation or prayer. But the Baal Shem Tov teaches that there is holiness -- there is God -- even in those recurring thoughts which seem to get in the way of prayer. Indeed: the machshevot zarot ("strange" or "foreign" thoughts) which get in the way of prayer are themselves a doorway into deeper prayer. They offer me an opportunity to make prayer out of my very distractions.
The obstacle itself becomes the door. This is a classic Hasidic move. Instead of feeling that gashmiut (physicality) is something which keeps us from God, we can use our very embodiment to drive our service to the One. Instead of seeing our mis-steps (or "sins") as things which distance us from God, we can see them as the first step toward making teshuvah, turning toward God again -- because when we fall away from God, we stimulate our own yearning to rise and reconnect. (Tanya / Likkutei Amarim, ch. 7)
The obstacle itself becomes the door. Or, phrased another way (as I learned it from my teacher Jason Shinder of blessed memory), whatever gets in the way of the work is the work. (He was talking about poetry, but -- as I've said here many times before -- I find it a valuable teaching about spiritual life, too.) The hope or fear or yearning which gets in the way of my prayer can become the substrate of my prayer. The desires, anxieties, and thoughts which might appear to disconnect me from God become my way to connect.