Fixed some bugs with reading from database