cleanup and add logging so is more understandable