More code optimization and one really ugly function.  #342