cleaned up error handling a little