refactor to align with golang implementation