Integrated nnilakh's code