moved error handling in front of actual processing