handling boundary conditions