TAkes to long to run - need to get rid of ugly nested loops