Fix issue #49

Also improved debugging when running in foreground more