Some refactorings

These are some refactorings, mainly to remove some code redundancies between the code for the master and the slaves.

I've been running this now on our production system for 2 month without any issues