Modified the code to include data logging