fix a bug, add utility functions and tests