improved function to get integer from string