Improved getTownId() function