refactoring finished

try catch finally to prevent resource leak