Updated with logging and formatting