reformat code
clarify the impl
add tests