Complete restructure of code to follow pep8 guideline

Also incroporated as fucntions to make code more clear