Rearrange listener and serving code