Rewritten to support GLM. Currently a bit buggy :(
Fixed buggy behaviour.