Version 2.0, complete rewrite and fixes #1.