Updated code with timer