Fix gcc/Qt5.0 build and mend some bugs
Fix valgrind warnings