Cleaned up the codes, changed to listeners to support more events, and handle error messages better.