Moved event handling logic out of datepicker