moved declarations
mini cleanup
moved calculation into function