modified and moved repeated code to one file