modularized code and added HEAD request support