Fixed variable declarations, and a few bugs