better error handling.
removed unnecessary code.