Merging Yoav's code