refactor this code