converting spaghetti to regular expressions to close #1