add debug flag and guard logging