Refactoring existing code

Main purpose is to increase code reuse, prevent direct database access and improve performance as multiple database queries are avoided

Important is 0014e63