refine code and add duration option