Complete rewrite to be more efficient and easier to use.