Code de-duplication, formatting fixes, some more logging