Restructuring js a bit