Add custom exception and fix minor logging bug