Refactored workflow code and tests