Refactored to use hook_schema for installing new table. Tidied up remaining code logic.