Added better exception  handling.