Rebased for sage-5.9