Database functionality - fixed bugs