Pretty happy with this function, too lazy to add more tests