refacotor the code