Refactor perl script to make it more usable