UI code & some buffer management stuff.

Have a look at it now.