Fix db bug and reduce repetition