fix 4063: optimizing the code, removing code duplicates