fix a selection bug, also add more unit tests