added error handling, removed unnecessary head/wc combination