moving handler code into its on function