added more checks, error messages, and escapes.