turned everything into functions for easier readablity, added some comments