B4 algorithm coding