Added some error handling in javascript