add some comment and more elegant solution