refactoring, improve syntax, remove globals