Code cleanup

I have been trying to clean up code to make sense of changes required for CRM-14197 - have broken off these few changes as a small chunk for easier review