refine functions and tests