decrease time windows and improve code