refactored cup code