Narrowed down the section of code that is creating problems with the database