made some code clearer and made logging better