Fix bugs and update test