Completed regex and minor refactoring