moved functions to end of file
added daemon and logging system for it