added safety at output, updated comments