Cleaned up error checking and removed debug code.