Simplify and refactor the code