Added some extra error handling.