Added onerror and some cleanup.