Rewrite and Added Tests. Function fails randomly. Have to check if this is my fault or unreliable data from OS-X