refactor onerror/onload