fixed formatting and main looping logic