Cleanups/fixes
Full rewrite
Minor cleanups