Problem with angles between lat and longitudes when using the atan function

Apologies I thought that part contained everything needed to help, I suppose not.
I've edited my original post to show all of the code now.