Restore changes removed from failed merge

Someone needs to double checks if this fixes everything and doesn't introduce new issues :-\