fix ap bugs and add test