made script more modular and it now accepts target directory