fixed logging, added some logic