Added a validate! function and started restructuring error handling code.