Fixed reading and writting to database so sessions get properly saveed and restored