Fixed all the tests and the gateway stuff for now. @dawen: Please review.