Actions are by intentions
إِنَّمَا الْأَعْمَالُ بِالنِّيَّاتِ، وَإِنَّمَا لِكُلِّ امْرِئٍ مَا نَوَى
Innama-l-a'malu bi-n-niyyat, wa innama li-kulli-mri'in ma nawa.
"Actions are judged by intentions, and every person will have what they intended."
Narrated by Umar ibn al-Khattab. Sahih al-Bukhari 1, Sahih Muslim 1907 · Nawawi's Forty · #1
A short reflection
The first hadith in Bukhari. Before the deed, check the heart — good deeds with bad intentions don't climb very high.