Fixes to my bad JS code. Add logging for errors and sorting operation.