Fixed bugs and some improvements