Split overlong function
REmove some redundancies
Small cleanups
small readability improvement
Small cleanup
Some more cleanups
Split overlong function
Split overlong function
Small cleanups