Added input bounds check