Added a lot of error handling.