re-organized code in script