Fixed some syntax and logic issues