Used casting to int instead of regexes (where possible) and added error message.