re-wrote code, onend doesn't seem to work for whatever reason.