refine code, use macro to remove the duplicate code