Refactoring for better error messages.
Added better error messages.