Functions and fixed a couple of errors