Moved some code to make it more reliable

I remember encountering erroneous stopPlaying events years ago and this fixed it
Formatting