Refractor some code