final script nearly done. Still need debugging and further design.