should simplify function (number of lines) and make modular
should simplify function (number of lines) and make modular