Fix bugs and add missing file