Refactor logic to be a bit cleaner