Sepreated onRequest function

And clean up.