Refactored to allow better use from outside