Improving javascript using AS API