Fixed several type errors identified by Flow