Improve limit/offset handling

Related to #853