optimized, and add test case