Improved implementation for staying within world boundary