Refactor to make it a bit easier to understand