refactored code so that we are not depending on db structure