Refactor a bit of logic