Address many pylance errors. Rewrite a few functions, will require testing to ensure functionality remains the same.