Refactored JS code a little