finished working out logic