Fixed some bugs which prevents work in '-d' mode