Various improvements

Some comments:

3f72b3e - I haven't actually checked whether open() or codecs.open() should be used - this patch just makes the use consistent for all the modes by using codecs.open() but it should be verified this is the right way by looking at the C or Java API.

a16063b - It's basically Method 3 from http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python. The previous one was Method 2.