Use a separate function for mod operation to avoid repeated evaluation