added check in hasTermFirst
More bug fixes