Functional

It now works for SMA's but is very dirty