Rewrote as class, various fixes.