Daemon bug fixed

There were an infinite loop when a program was a daemon
Added some tests to avoid NPE