added safe_read/safe_write
minor code restyling