more work on tidying up function generation