Updated with better logging