Added WaitForTransaction() function.
cleanup and refactoring