Take payoad preparation out into a seperate function, add some comments