Much cleaner JS code for detecting change