optimizing code & handling of missings

You may find find that useful. 

Since your indentation does not comply with pep8, you have to go through the single commits to track my changes.