Fixed bugs and add debug lines