improvements on z-axis algorithm