Clean master code

Cleaned code following the python guide style.
Changed some try: expect KeyError: for if key in dict.
Simplified some functions.
Add __name__ == '__main__'