Fix bug/major issues reported by scrutinizer