fix a bug, add some tests