Added new z calculation code