Fixed performance of reference plane generation (now quick in both Firefox and Chrome) and cleaned up the code significantly