Implemented basic validation.

Lots of cleanups throughout.