refactoring of result printing, exception catching, proper using globally cached variables