Lots of changes, figured out some scoping issues, need to redo some parts to use apply() better