added boundary condition handling