refactor the code to work normal