Refactor a lot of code to utilities