Fix typos
Cleanup of functions and loops