Refactor so it can be run from batch file