refactored and indented properly  according to python standard