Added plugin support

Made the whole thing more exstensible by added plugin support.

Fixed a few bugs.