Refactor to allow using this as a module