this entire function is a duplicate of the one found in file.rb - this should refactored