handle boundary conditions