Fix bugs and clear debug code