Implemented logic and tests