Fixed some None warnings and some code rearranging