redid a lot of things to work as functions

useful for future.