Rewritten for compatibility with 3.8.1 by Leo Caseiro