Removed scipy dependency by stealing code from them (temporarily)