fix boundaries issue and add some tests