Tima's code integrated