implemented logic and tests