added logging and some cleanup