JS: Added more error checking