fix some typos
some bug fixes, more robust checking
add example and test cases