Updated version with additional changes, min/max z per block, minor optimization from gist