whole logic modified for new version of firebase