Refactoring redundant code and fix syntax errors #2