Fix bugs detected by integration test