Fix bug in handling of z regions
Add unit tests