Fixing some logical bugs, also added if __name__ == "__main__".