Error handling enchancements. Added some utility functions.