finished freaking perl implementation