But sometimes I keep having wandering thoughts, what to do in such situations?

When we get up in the morning, submit yourselves wholly (body, soul and spirit) to God.Ask the Holy Spirit to guide you throughout the day.

Pray to God for a sound mind and spiritual discernment – ie, to know if a thought or matter is from God or not. Holy Spirit will help you. From your part, once you become aware that the matter is from devil, purposefully deviate your mind to something Godly. He will help you to have control over thoughts.