Fixed segment sorting and moved the JS into separate files