Fixed errors so that the code can be compiled.

And some minor improvements