Refractor the code