Refactor by using improved resource managment