move boundary protection to main loop