Slightly cleaned up error handling. Still a long way to go.