simplify and added logging