refactoring to work when called by another program