Refactor and cleanup code, fix regular expression.