first working script ; need to refine it a bit