Also made this a separate function to keep the code DRY.