Continue refactoring and bug-fixing.
Pull out wait for acknowledge into function and a few bug fixes.