optimise algorithm to remove duplicate box
add more comments