Database code working for the most part