Complete rewrite of YUI

Rewrite of YUI to make easier to read and modify. Comments added
throughout