Fix some bugs and some warnings