adding all old code - this needs tidying-up