refractor a bit of code