Implement waitresp handling and do some code clean-up.