Slightly simplified implementation.