Added a lot of error protection.