Improving logic and tests