Optimize code to avoid db requests