javascript: refactoring - draft version