There are similar code in several parts (like line 371, 628) , it is good to have just one code to do it.