Refactoring Firebase code