removed dependence on temporary files; refactored