Added rest of logic with tests