Cleaned up js, added new error-handling