added better logging and comments