Rebased to v3.0.0 code