added logic and fixed test