Refactor ambrose code to make it more modular and share code