Added better exception handling