added scopes to solve the bug I was having and broke everything up into functions for clarity