Check this out for question 1.

Not sure about question 2, but this may point you in the right direction.

Hope this helps,