Fixed error handling. Some style modifications.