Fixed some code flaws