refactored
added logging