Clean up code a bit and add some comments. iOS should be completely supported but needs testing.