renamed variables and function names to be more consistent

lot of rewrite, but not much different functionality. Hopefully, easier
to manage now.