Extracting the logic into one function