This is crap, needs rewrite
Misc. fixes