fix syntax errors and general refactoring of code