added logging, fixed some queries