Fixed some logical errors