Added better error handling mechanism.