move functions around
rename functions and improve comments
rename variables to improve readability