Started refactoring and adding more features.

Hi.

I made a bunch of changes to the code but kept the current API intact.  I plan on doing more work on it as time goes by.

Please review and accept if suitable.