integrating nmmadaan's code