Fix limiting condition issue and add tests