added comments
changed everything to a function