More robust edge condition handling