BUG: fixed boundary checking