restructure the code and fix a few issues related to error handling
add more unit testing for error handling for channel mode
remove the unused code