Created a separate function to compute Z.