code still in progress based on redshift problem
tidied up code for inspection