Refactored much code to bring functions out of board.on loop and sockets loop