massive rewrite

Massive rewrite