Code logic was intermingled with function declarations. Moved all