copied over more from previously written code by Ruben
copied over more from previously written code by Ruben