added logging but it could be probably better