debug version (not finished, not tested)