rewrite: main functionality restored