added logic and tests