adapted after comments from Guiliano