Cleaned up code. Now relies on the moreutils package