clean up, error handling improved