fixing bugs, adding debug logging