Wrote some incomprehensible function which i will no doubt regret when i look at it again in a month's time. Untested as of yet