revised lots of code to make this more functional