Refactorized javascript