Refactored for easy embedding