Follow programming guidelines.
Code rearranged in methods for readability
Added terminate message and fix an error with previous commit