Restructured javascript