aligned with java implementation