updated code according to fafhrd91's comments