Fix a bunch of bugs in range compilation and checking