If think that you can lower on one or two level of indentaiton in this function by returning None at the end of each if/elif block. Indentation really make the code harder to understand I think.