refectored server code and ensured events are sent after server actions