Refactore the code