Removed some test code and made it a little more robust