I made a simple melody in FL studio that was in C Major. The melody consisted of two phrases both of which started on D. The only difference between the two is the second phrase ended on C (the tonic of C major). The only problem is, it didn’t feel resolved ending on the tonic like I thought it would. When I changed the last note of the tonic to a D it sounded resolved even though D is not the tonic of C Major. After looking further into it, I realized that all the notes I used were also in D Dorian as well as C Major. So, my question is what defines a melody as being in a certain key to the listener? What makes the listener want to hear notes in a certain scale within a melody or resolve on the tonic of a certain scale?