Logic and syntax fixes