Ad with a lot from this module - I took the code from https://gist.github.com/dimus/335286 and didn't optimize it. Well - guess I'm doing it now.