added logging & refactored