Rearranged rest handler code