Refactored code to allow easier integration