code cleanup, error flow handling