simplified and improved version.