ported improved boundary handling