Added boundery check for ball detection.