return nil resource on error

this change also includes some additional minor code cleanup

fixes #58