rearranged to make it a lib