Bugs fixed - previous version did not work, missing a few declarations.