fix bugs and implement some improvements