Fixed copy paste errors. Better error handling.