add resource protection
code clean up