Refactor some code, adding comments and clean some code for pep8