change code to more concise way

try to use closure to rewrite all code