insertByWeight convert to use pattern matching. remove dead code