improved handling of boundaries