Major rewrite. Fixes all over the place