Modify logic and add tests