Major rewrite and additions