Refactored firebase chat code