splitting up code in different definitions
added logging