Merged with Tibor's code