script cleaned and unified for both command line usage and webapp usage