Refactoring it a bit for the logic.