Added more thorough error crecking.