Re-factored code for doctrine