generic implementation of Daciuk's algorithm