Fix bugs; improve Qt5 compatibility

Fix various bugs and improve some parts of code.