Front-end JS code cleanup (or messup, if the slightly more convoluted code is any indication :P)