Improved boundary check