refactoring and cleanup of JS code