logic adapted (hopefully correct?)